<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include APPLICATION_PATH . "/models/Feedback.php";
include APPLICATION_PATH . "/models/User.php";
include APPLICATION_PATH . "/models/Admin.php";
include APPLICATION_PATH."/models/News.php";
class FeedbackBKController extends Zend_Controller_Action{
      public function init() {
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $this->view->aConfig = $this->aConfig;
        $this->modelMapper= new Model_FeedbackMapper();
        $this->model= new Model_Feedback();
        $this->UsermodelMapper= new Model_UserMapper();
        $this->Usermodel= new Model_User();
         $this->AdminmodelMapper= new Model_AdminMapper();
        $this->Adminmodel= new Model_Admin();
        //truyen
        $this->NewsmodelMapper= new Model_NewsMapper();
        //end truyen
    }
    public function indexAction(){
        $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
         $trang = $this->_getParam("trang","1");
         $this->view->trang = $trang;
         $this->view->items=$this->model;       
         $this->view->htlmQuestion=  GlobalLib::listQuestion(2,$trang);  
        //phan trang trong zend php           
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAll());
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            $string=$this->_getParam("list_selectQuestion","");
            $t=  substr($string,0, strlen($string)-1);
            $this->view->list_id=$t;  
          
            //Đưa nội dung câu trả lời ra           
              $array_id =  explode(',',$t); 
              $count=0;
              foreach($array_id as $value){
                $id= $value;
                 if($count==2){
                       
                    $this->modelMapper->find($id,$this->model);
                    $this->view->getId=$this->model->getId();
                    $this->view->getTitle=$this->model->getTitle();
                    $this->view->getAnswer="Nội Dung Chung:".$this->model->getAnswer();
                     
                     break;
                                 
                 }
                $this->modelMapper->find($id,$this->model);
                $this->view->getId=$this->model->getId();
                $this->view->getTitle=$this->model->getTitle();
                $this->view->getAnswer=$this->model->getAnswer();
                $count++;
               
            }
         
//        ////////////////////////////
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."default/feedback/index";            
            //tao mot mang id         
            if(isset($_POST["id"])){
               $array_id =  explode(',',$_POST["id"]);              
            } 
            ///
            foreach($array_id as $value){
                $id= $value;
                $this->modelMapper->find($id,$this->model);
                $getId=$this->model->getId();
                $getTitle=$this->model->getTitle();
                $getAnswer=$this->model->getAnswer();
                $getUserId=$this->model->getUser_Id();
                $getAdminId=$this->model->getAdmin_Id();
             
//                echo $getQuestion;
//                echo 'vi tru bat dau SOTU:\n '.$viTri_SOTU;   
//                echo 'vi tri bat dau NOIDUNG: \n'.$vitri_NOIDUNG;
//                echo '\n'.          $ID = substr($getQuestion,3,$viTri_SOTU-3);
//                echo '\n'.          $SOTU = substr($getQuestion,$viTri_SOTU+5,$vitri_NOIDUNG-$viTri_SOTU-5);
//                echo '\n'.          $NOIDUNG = substr($getQuestion,$vitri_NOIDUNG+8,strlen($getQuestion)-$vitri_NOIDUNG-8 );   
                ////
                if(empty($getId)){
                    $this->_redirect($redirectUrl);
                }
                /// 
                $this->model->setId($getId);
                $this->model->setTitle($getTitle);
                $this->model->setAnswer($getAnswer);
                $this->model->setUser_Id($getUserId);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By($getUserId);
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId()); 
                
                   //neu la cau thong bao chung.tat ca cac user trong khu vuc deu co quyen tra loi
                if($getUserId==0){
                    //
                    $getQuestion=$this->model->getQuestion();
//                    if($getQuestion==""){
//                        $getQuestion=$getQuestion;
//                    }else    
//                    {
                        //cat ra
                    $viTri_SOTU=strpos($getQuestion,"SOTU:");
                    $vitri_NOIDUNG=strpos($getQuestion,"NOIDUNG:");
                    $ID = substr($getQuestion,3,$viTri_SOTU-3);
                    $SOTU = substr($getQuestion,$viTri_SOTU+5,$vitri_NOIDUNG-$viTri_SOTU-5);
                    $NOIDUNG = substr($getQuestion,$vitri_NOIDUNG+8,strlen($getQuestion)-$vitri_NOIDUNG-8 );
                        //them gia tri vao
                        $user_login =GlobalLib::getUserLogin();
                        $id_us=  $user_login["id"];
                        $ID=$ID.$id_us.",";
                        $SOTU=$SOTU.str_word_count(strip_tags($_POST["answer"])).",";
                        $NOIDUNG=$NOIDUNG.strip_tags($_POST["answer"])."";
                        //gan lai va luu vao csdl
                    $getQuestion="ID:".$ID."SOTU:".$SOTU."NOIDUNG:".$NOIDUNG;
//                    }
                        //
                    if(strlen($_POST["answer"])>0){
                       $this->model->setQuestion($getQuestion);
                    } 
                }else{
                     if(strlen($_POST["answer"])>0){
                        $this->model->setQuestion(strip_tags($_POST["answer"]));
                     }      
                }
                              
                   
                $this->model->setAdmin_Id($getAdminId);
                $this->modelMapper->save($this->model);
            }    
            
            $this->_redirect($redirectUrl);
            }     
           $this->view->item= $this->model; 
    }
    public function listanswerAction(){        
        //phan trang trong zend php
          $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("title","title not like 'TB%'","modified DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
             $string=$this->_getParam("list_selectQuestion","");
            $t=  substr($string,0, strlen($string)-1);
            $this->view->list_id=$t;
    }
     public function messgerAction(){       
        //phan trang trong zend php
           $this->view->items1=$this->NewsmodelMapper->publicpublished5tin(1);
           $paginator = Zend_Paginator::factory($this->modelMapper->fetchAll());
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            $string=$this->_getParam("list_selectQuestion","");
            $t=  substr($string,0, strlen($string)-1);
            $this->view->list_id=$t; 
            $user_login =GlobalLib::getUserLogin();
            $id_dn = $user_login["id"];
            $this->view->my_list_kinhdoanh = $this->AdminmodelMapper->fetchAll_KD_USER($id_dn);
            $myarray_id =  explode(',',$string);
            $list_user = "";
            foreach ($myarray_id as $my_id)
            {                
                if($my_id != "")
                {
                    $list_user = $list_user.GlobalLib::getName("user",$my_id, "username").";";                    
                }
            }            
            $this->view->list_user = $list_user;
        ////////////////////////////
        if($this->getRequest()->isPost()){
            $redirectUrl = $this->aConfig["site"]["url"]."default/feedback/listanswer";
        //tao cau thong bao
        if(strlen($_POST["id"])<=0){
                //khong chon danh sach thi gui cau hoi
                 if(strlen($_POST["title"])>0){$this->model->setTitle($_POST["title"]);}
                 if(strlen($_POST["question_kd"])>0){$this->model->setQuestion(strip_tags($_POST["question_kd"]));}               
                 $user_login =GlobalLib::getUserLogin();
                 $this->model->setUser_Id($user_login["id"]);
                 $this->model->setCreated(date("Y/m/d H:i:s"));
                 $this->model->setCreated_By($user_login["id"]);
                 $this->model->setModified(date("Y/m/d H:i:s"));
                 $this->model->setModified_By(GlobalLib::getLoginId());          
                 $this->model->setAnswer("");
                 $this->model->setAdmin_Id(0);
                 $this->modelMapper->save($this->model);
        }else {
        //chon thi tao cau thong bao
            if(strlen($_POST["id"])>0){
                   $array_id =  explode(',',$_POST["id"]);              
            }
        
            ///
            foreach($array_id as $value){
                $id= $value;
                if(strlen($_POST["title"])>0){
                    $title='TBD'.$_POST["title"];
                    $this->model->setTitle($title);
                }
                if(strlen($_POST["question_kd"])>0){
                    $this->model->setQuestion(strip_tags($_POST["question_kd"]));
                }               
                $user_login =GlobalLib::getUserLogin();
                $this->model->setUser_Id($user_login["id"]);
                $this->model->setCreated(date("Y/m/d H:i:s"));
                $this->model->setCreated_By($user_login["id"]);
                $this->model->setModified(date("Y/m/d H:i:s"));
                $this->model->setModified_By(GlobalLib::getLoginId());          
                $this->model->setAnswer("");                          
                $this->model->setAdmin_Id($id);
                $this->modelMapper->save($this->model);
                }
           }
            $this->_redirect($redirectUrl);
        }     
           $this->view->item= $this->model;   
    }
    public function addressAction(){ 
        //lay user dang nhap
            $user_login =GlobalLib::getUserLogin();
            $id_dn = $user_login["id"];            
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("substring(title,3)","title  like 'TBT%' and( user_id='0' or user_id='". $id_dn."')","id DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            //danh sach nhung kinh doanh theo user
            $this->view->list_kd_user =$this->AdminmodelMapper->fetchAll_KD_USER($id_dn);            
            if($this->getRequest()->isPost()){
                  if(strlen($_POST["list_question"])>0){                    
                     $string_id=$_POST["list_selectQuestion"];
                     echo $string_id; 
                     exit();
              } 
            }
    }
     public function address1Action(){ 
        //lay user dang nhap
            $user_login =GlobalLib::getUserLogin();
            $id_dn = $user_login["id"];            
            $paginator = Zend_Paginator::factory($this->modelMapper->fetchAllTBL("substring(title,3)","title  like 'TBD%' and( user_id='0' or user_id='". $id_dn."')","id DESC"));
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
            //danh sach nhung kinh doanh theo user
            $this->view->list_kd_user =$this->AdminmodelMapper->fetchAll_KD_USER($id_dn);            
            if($this->getRequest()->isPost()){
                  if(strlen($_POST["list_question"])>0){                    
                     $string_id=$_POST["list_selectQuestion"];
                     echo $string_id; 
                     exit();
              } 
            }
    }
    public function listuserAction(){           
          $paginator = Zend_Paginator::factory($this->AdminmodelMapper->fetchAll());
            $paginator->setItemCountPerPage(3);        
            $paginator->setPageRange(3);
            $currentPage = $this->_request->getParam('page',1);
            $paginator->setCurrentPageNumber($currentPage);
            $this->view->data=$paginator;
     }
    
}